/*
 * queue.c
 *
 *  Created on: Apr 2, 2012
 *      Author: Tri_D
 */


#include "queue.h"
#include "main header.h"

void init_que512(que_512 *q)
{
	uint16_t count_ = 0;
	q->newest = 0; 
	q->oldest  = 0;
	for(count_= 0; count_ < 512; count_++)
		q->que[count_] = 0;	
}


void put_que512(que_512 *q, uint8_t c)
{
	uint16_t n = q->newest;
	uint16_t o = q->oldest;
	q->que[n++] = c;
	if (n >= 512)	n=0;
	if (n == o)
		if ( (++o) > 512 ) o = 0;
	q->oldest = o;
	q->newest = n;	
}

uint8_t get_que512(que_512 *q)
{
	uint16_t n = q->newest;
	uint16_t o = q->oldest;
	if (n == o) return 0;
	n = q->que[o++];
	if (o >= 512) o = 0;
	q->oldest = o;
	return (uint8_t) n;	
}


uint16_t que_count512(que_512 *q)
{
	uint16_t n = q->newest;
	uint16_t o = q->oldest;
	uint16_t c;
	if (n >= o) c = n-o;
	if (o > n) c = (512 - o) + n;
	return c;	
}


BOOL get_chr512(que_512 * q, uint8_t * pd)
{	
	if (que_count512(q) > 0)
	{
		* pd = get_que512(q);
		return TRUE;
	}
	else
		* pd = 0;
		
	return FALSE;
}